<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="login-background">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
    </div>

    <div class="login-content">
      <!-- 左侧装饰 -->
      <div class="login-left">
        <div class="brand">
          <div class="brand-info">
            <h2 class="brand-title">工作流平台</h2>
            <p class="brand-desc">让业务流程管理更简单</p>
          </div>
        </div>
        <div class="decoration">
          <img src="@/assets/login-decoration.svg" alt="decoration" class="decoration-img" />
        </div>
        <div class="feature-list">
          <div class="feature-item">
            <el-icon><Connection /></el-icon>
            <span>可视化流程设计</span>
          </div>
          <div class="feature-item">
            <el-icon><Operation /></el-icon>
            <span>灵活的流程控制</span>
          </div>
          <div class="feature-item">
            <el-icon><DataAnalysis /></el-icon>
            <span>全面的数据分析</span>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-right">
        <div class="login-form-wrapper">
          <h3 class="welcome-text">欢迎登录</h3>
          <p class="sub-title">工作流平台助您轻松管理业务流程</p>

          <el-form
            ref="formRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            size="large"
          >
            <el-form-item prop="tenantId">
              <el-input
                v-model="loginForm.tenantId"
                placeholder="请输入租户ID"
                :prefix-icon="OfficeBuilding"
                clearable
              />
            </el-form-item>
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                :prefix-icon="UserFilled"
                clearable
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
                clearable
                @keyup.enter="handleLogin"
              />
            </el-form-item>

            <el-form-item class="login-btn">
              <el-button
                :loading="loading"
                type="primary"
                class="login-submit"
                @click="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import {
  UserFilled,
  Lock,
  OfficeBuilding,
  Connection,
  Operation,
  DataAnalysis,
} from '@element-plus/icons-vue'
import { login } from '@/api/auth'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const formRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive({
  tenantId: '',
  username: '',
  password: '',
})

const loginRules = {
  tenantId: [{ required: true, message: '请输入租户ID', trigger: 'blur' }],
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const handleLogin = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await login(loginForm)
        // 保存token和租户信息
        userStore.setToken(res.data.token)
        userStore.setTenantId(loginForm.tenantId)
        userStore.setUsername(res.data.user.username)

        ElMessage.success('登录成功')

        // 登录成功后跳转到上次退出时的页面或首页
        const redirect = userStore.lastPath || '/'
        // 跳转后清除记录的路径
        userStore.setLastPath('')
        router.push(redirect)
      } catch (error: any) {
        console.error('登录失败:', error)
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f0f7ff 0%, #e6f7ff 100%);
  position: relative;
  overflow: hidden;
  padding: 20px;
}

.login-background {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 0;
  background: radial-gradient(
      circle at 10% 20%,
      rgba(216, 241, 230, 0.46) 0%,
      rgba(233, 226, 226, 0.28) 110.2%
    ),
    linear-gradient(45deg, rgba(22, 119, 255, 0.05) 0%, rgba(104, 74, 255, 0.05) 100%);
}

.shape {
  position: absolute;
  border-radius: 50%;
  filter: blur(40px);
}

.shape-1 {
  width: 500px;
  height: 500px;
  background: linear-gradient(140deg, rgba(24, 144, 255, 0.1), rgba(104, 74, 255, 0.08));
  top: -100px;
  right: -100px;
  animation: float 8s ease-in-out infinite;
}

.shape-2 {
  width: 300px;
  height: 300px;
  background: linear-gradient(220deg, rgba(250, 84, 28, 0.08), rgba(229, 72, 77, 0.05));
  bottom: -50px;
  left: -50px;
  animation: float 6s ease-in-out infinite reverse;
}

.shape-3 {
  width: 250px;
  height: 250px;
  background: linear-gradient(180deg, rgba(82, 196, 26, 0.08), rgba(47, 194, 91, 0.05));
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation: pulse 4s ease-in-out infinite;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-20px);
  }
}

@keyframes pulse {
  0%,
  100% {
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    transform: translate(-50%, -50%) scale(1.1);
  }
}

.login-content {
  display: flex;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow:
    0 4px 6px rgba(0, 0, 0, 0.02),
    0 10px 30px rgba(0, 0, 0, 0.04),
    inset 0 0 0 1px rgba(255, 255, 255, 0.5);
  overflow: hidden;
  width: 900px;
  max-width: 100%;
  position: relative;
  z-index: 1;
  margin: auto;
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, rgba(240, 247, 255, 0.8) 0%, rgba(230, 244, 255, 0.9) 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 460px;
}

.brand {
  position: relative;
  z-index: 1;
}

.brand-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.brand-title {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, #1677ff 0%, #4096ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  margin: 0;
}

.brand-desc {
  font-size: 16px;
  color: var(--el-text-color-secondary);
  margin: 0;
}

.decoration {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 0;
}

.decoration-img {
  width: 100%;
  max-width: 420px;
  height: auto;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.1));
}

.feature-list {
  display: flex;
  gap: 12px;
  margin-top: 20px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 10px;
  backdrop-filter: blur(4px);
}

.feature-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--el-text-color-regular);
  font-size: 14px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  transition: all 0.3s;
}

.feature-item:hover {
  transform: translateY(-2px);
  background: white;
  box-shadow:
    0 4px 12px rgba(0, 0, 0, 0.05),
    0 0 0 2px rgba(22, 119, 255, 0.1);
}

.feature-item .el-icon {
  font-size: 18px;
  background: linear-gradient(135deg, #1677ff 0%, #4096ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.login-right {
  width: 360px;
  display: flex;
  align-items: center;
  background: white;
  position: relative;
  padding: 40px;
}

.login-form-wrapper {
  width: 100%;
}

.welcome-text {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, #1677ff 0%, #4096ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 8px;
}

.sub-title {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 40px;
}

.login-form :deep(.el-form-item) {
  margin-bottom: 24px;
}

.login-form :deep(.el-input__wrapper) {
  box-shadow: none;
  border: 1px solid rgba(22, 119, 255, 0.15);
  padding: 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.8);
}

.login-form :deep(.el-input__wrapper:hover) {
  border-color: rgba(22, 119, 255, 0.3);
  background: white;
}

.login-form :deep(.el-input__wrapper.is-focus) {
  border-color: #1677ff;
  background: white;
  box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.1);
}

.login-form :deep(.el-input__prefix) {
  margin-right: 12px;
}

.login-btn {
  margin-top: 40px;
}

.login-submit {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 10px;
  background: linear-gradient(135deg, #1677ff 0%, #4096ff 100%);
  border: none;
  transition: all 0.3s;
}

.login-submit:hover {
  transform: translateY(-2px);
  box-shadow:
    0 4px 12px rgba(22, 119, 255, 0.3),
    0 0 0 3px rgba(22, 119, 255, 0.1);
  background: linear-gradient(135deg, #1668dc 0%, #3a87ff 100%);
}

/* 响应式布局 */
@media screen and (max-width: 1024px) {
  .login-content {
    width: 90%;
  }

  .login-left {
    min-width: 0;
    padding: 32px;
  }

  .login-right {
    padding: 32px;
  }
}

@media screen and (max-width: 768px) {
  .login-left {
    display: none;
  }

  .login-right {
    width: 100%;
  }

  .login-content {
    width: 100%;
    max-width: 400px;
  }

  .shape-1,
  .shape-2,
  .shape-3 {
    display: none;
  }
}
</style>
